﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using ICalendar.CalendarProperties;
using ICalendar.ValueTypes;


namespace ICalendar
{
    class Program
    {
        static void Main(string[] args)
        {
            var eventos = new EventCollection();
            var evento = new Event("EVENTO TESTE DO ALAN GENERATOR TABAJARA", DateTime.Now.AddMinutes(1),
                                   DateTime.Now.AddHours(2))
                             {
                                 Location = "Sala de Reunião do 8o Andar",
                                 Description = "blablabla evento teste",
                                 Organizer =
                                     new Organizer(new MailAddress("operninha@gmail.com", "Alan Santos Rodrigues"),
                                                   new MailAddress("agenda-sharepoint@ons.org.br", "Agenda Sharepoint")),
                                 Geo = new Geo(37.386013f, -122.082932f),
                                 Status = EventStatusValue.Confirmed,
                                 Transparent = false,
                                 RecurrenceId = new RecurrenceId(DateTime.Now) {Range = Range.ThisAndFuture}
                                                       
                             };

            var evento2 = new Event("EVENTO2", DateTime.Now, DateTime.Now.AddHours(2))
                              {
                                  Location = "Sala de Reunião do 20o Andar",
                                  Organizer = new MailAddress("operninha@gmail.com", "Alan Rodrigues") 
                              };

            var trigger = new Duration() {Minute = 4,  TimeLine = TimeLine.Before};//DateTime.Now.AddMinutes(1

            evento.Alarms.Add(new Alarm(ActionValue.Display, trigger)
                                  {
                                      Duration = new Duration(DurationType.Time) {Day = 5, Minute =  4, TimeLine =  TimeLine.After},
                                      Description = "Reminder",
                                      TriggerRelationship = TriggerRelationship.End
                                  });
            eventos.Add(evento);
            eventos.Add(evento2);

            var calendar = new Calendar(MethodNames.Request) { ProductId = "-//Microsoft Corporation//Outlook 12.0 MIMEDIR//EN" };
            var a = new Attendee(new MailAddress("arodrigues@ons.org.br", "Alan Rodrigues")){IsReplyRequested = false, ParticipationStatus = ParticipationStatus.Accepted, Role = Role.Chair};
            var aa = new Attendee(new MailAddress("gmata@ons.org.br","Gustavo Mata")) {IsReplyRequested = true, Role = Role.Required};
            var aaa = new Attendee("agcunha@ons.org.br") {IsReplyRequested = true,  Role = Role.Optional,  };

            aaa.CustomXParameters.Add("X-TESTE-CALENDAR", "YES");

            string vCalendarBegin, vCalendarBeginValue;
            XParameterCollection xparamsBegin;
            Util.Read("BEGIN:VCALENDAR", out vCalendarBegin, out vCalendarBeginValue, out xparamsBegin);

            string export = aaa.ToString();

            Attendee import = new Attendee( new MailAddress("teste@teste.com"));
            import.FromString(export);
       
            var fs = new FileStream(@"c:\Person2.xml", FileMode.Create);
            //XmlWriter xw = XmlWriter.Create(fs,new XmlWriterSettings() {ConformanceLevel=ConformanceLevel.Auto});
            XmlWriter xw = XmlWriter.Create(fs);
            aaa.WriteXml(xw);
            //binaryFmt.Serialize(fs, p);
            xw.Close();
            fs.Close();

            var fs2 = new FileStream(@"c:\Person.xml", FileMode.Create);
            //XmlReader xr = XmlReader.Create(fs);
            //aa.ReadXml(xr);
            //xr.Close();

            
            XmlSerializer s = new XmlSerializer(typeof(Attendee));
            
            Console.WriteLine("Testing for type: {0}", typeof(Attendee));
            XmlWriterSettings xws = new XmlWriterSettings();
            xws.Indent = true;
            xws.NewLineHandling = NewLineHandling.Entitize;

            s.Serialize(XmlWriter.Create(fs2,xws), aaa);
            fs2.Flush();
            fs2.Seek(0, SeekOrigin.Begin);
            object o = s.Deserialize(XmlReader.Create(fs2));
            fs2.Close();
            Console.WriteLine("Deserialized type: {0}", o.GetType());


            a.DelegatedFrom.Add("arodrigues@ons.org.br");
            evento.Attendees.Add(a);
            evento.Attendees.Add(aa);
            evento.Attendees.Add(aaa);
            
            calendar.Events.Add(evento);
            calendar.Events.Add(evento2);
            var icsFile = calendar.ToString();
            var arquivo = File.CreateText(@"c:\eventoTeste.ics");
            arquivo.Write(icsFile);
            arquivo.Close();


            //System.Xml.Xsl.XslCompiledTransform b = new System.Xml.Xsl.XslCompiledTransform(true);
            //b.Load(@"c:\ics2xml2.xslt");
            //var settings = new XmlWriterSettings();
            //b.OutputSettings.ConformanceLevel = ConformanceLevel.Auto;
            //System.Xml.Xsl.XsltArgumentList args = new System.Xml.Xsl.XsltArgumentList();
            //args.AddParam(""
           // b.Transform(@"C:\eventoTeste.ics",  @"c:\eventoTeste.xml",);
            
            //REQUEST PARTS
            var client = new SmtpClient("10.203.5.8");
            var mailMessage = calendar.CreateMailMessage();
            //MailAddress
            mailMessage.From = new MailAddress("agenda-sharepoint@ons.org.br");
            //mailMessage.To.Add("operninha@gmail.com");
            mailMessage.To.Add("arodrigue@ons.org.br");
            //mailMessage.To.Add("gmata@ons.org.br");
            client.Send(mailMessage);

            ////PUBLISH TO ORGANIZER
            calendar.Method = MethodNames.Publish;
            mailMessage = calendar.CreateMailMessage();
            mailMessage.From = new MailAddress("agenda-sharepoint@ons.org.br");
            mailMessage.To.Add("arodrigues@ons.org.br");
            ////mailMessage.To.Add("gmata@ons.org.br");
           //client.Send(mailMessage);

        }

    }
}
